package com.blog.personalblog.Validator;

import com.blog.personalblog.util.PhoneUtil;
import com.blog.personalblog.util.StrUtil;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
public class MobileNoValidator implements ConstraintValidator<MobileNo, String>{
    private boolean nullable;
    private boolean blankable;
    private String message;

    public MobileNoValidator() {
    }
    @Override
    public void initialize(MobileNo constraintAnnotation) {
        this.nullable = constraintAnnotation.nullable();
        this.message = constraintAnnotation.message();
        this.blankable = constraintAnnotation.blankable();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (value == null) {
            if (!this.nullable) {
                //禁止默认消息返回
                context.disableDefaultConstraintViolation();
                //自定义返回消息
                context.buildConstraintViolationWithTemplate("手机号不能为空").addConstraintViolation();
                return false;
            } else {
                return true;
            }
        } else if (value.length() == 0) {
            if (!this.blankable) {
                context.disableDefaultConstraintViolation();
                context.buildConstraintViolationWithTemplate("手机号不能为空白").addConstraintViolation();
                return false;
            } else {
                return true;
            }
        } else if (!PhoneUtil.checkMobile(value)) {
            context.disableDefaultConstraintViolation();
            context.buildConstraintViolationWithTemplate(StrUtil.defaultIfBlank(this.message, "手机号格式不对")).addConstraintViolation();
            return false;
        } else {
            return true;
        }
    }
}
